package Game;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.ImageIcon;

public class Fish {
    private int x;
    private int y;
    private int vx; // 水平速度
    private int vy; // 垂直速度
    private BufferedImage[] originalImgs = new BufferedImage[3]; // 原始图片
    private BufferedImage[] rotatedImgs = new BufferedImage[3]; // 旋转后的图片
    private int currentImgIndex = 0; // 当前显示图片的索引
    private int fishType; // 鱼的类型(1-6)
    private double rotationAngle = 0; // 旋转角度
    private Random random = new Random();
    private boolean isDead = false; // 表示鱼是否死亡的属性
    private int scoreValue; // 每种鱼对应的分数

    public Fish(int type) {
        this.fishType = type;
        initPositionAndSpeed();
        loadFishImages();
        updateRotation(); // 初始化旋转图片
        scoreValue = fishType;
    }

    private void initPositionAndSpeed() {
        // 从边界外随机位置生成
        int side = random.nextInt(4);
        switch (side) {
            case 0: // 从上边进入
                x = random.nextInt(1128);
                y = -100;
                vx = random.nextInt(5) - 2; // -2到2
                vy = random.nextInt(3) + 1; // 1到3
                rotationAngle = 90; // 头朝下
                break;
            case 1: // 从右边进入
                x = 1128 + 100;
                y = random.nextInt(844);
                vx = -(random.nextInt(5) + 1); // -5到-1
                vy = random.nextInt(5) - 2; // -2到2
                rotationAngle = 180; // 头朝左
                break;
            case 2: // 从下边进入
                x = random.nextInt(1128);
                y = 844 + 100;
                vx = random.nextInt(5) - 2; // -2到2
                vy = -(random.nextInt(3) + 1); // -3到-1
                rotationAngle = 270; // 头朝上
                break;
            case 3: // 从左边进入
                x = -100;
                y = random.nextInt(844);
                vx = random.nextInt(5) + 1; // 1到5
                vy = random.nextInt(5) - 2; // -2到2
                rotationAngle = 0; // 头朝右
                break;
        }
    }

    private void loadFishImages() {
        for (int i = 0; i < 3; i++) {
            Image img = new ImageIcon("img/fish" + fishType+"_" + (i + 1) + ".png").getImage();
            originalImgs[i] = toBufferedImage(img);
        }
    }

    private BufferedImage toBufferedImage(Image img) {
        if (img instanceof BufferedImage) {
            return (BufferedImage) img;
        }
        BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bimage.createGraphics();
        g2d.drawImage(img, 0, 0, null);
        g2d.dispose();
        return bimage;
    }

    private void updateRotation() {
        // 根据移动方向计算旋转角度
        double angle = Math.toDegrees(Math.atan2(vy, vx));

        // 只有角度变化超过5度才重新旋转，减少计算量
        if (Math.abs(angle - rotationAngle) > 5) {
            rotationAngle = angle;

            // 旋转所有三张图片
            for (int i = 0; i < 3; i++) {
                rotatedImgs[i] = rotateImage(originalImgs[i], rotationAngle);
            }
        }
    }

    private BufferedImage rotateImage(BufferedImage original, double angle) {
        int width = original.getWidth();
        int height = original.getHeight();

        // 计算旋转后所需的画布大小
        double radians = Math.toRadians(angle);
        double sin = Math.abs(Math.sin(radians));
        double cos = Math.abs(Math.cos(radians));
        int newWidth = (int) Math.floor(width * cos + height * sin);
        int newHeight = (int) Math.floor(height * cos + width * sin);

        // 创建足够大的画布
        BufferedImage rotated = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = rotated.createGraphics();

        // 设置旋转中心为画布中心
        g2d.translate(newWidth / 2, newHeight / 2);
        g2d.rotate(radians);
        g2d.translate(-width / 2, -height / 2);

        // 绘制原始图像
        g2d.drawImage(original, 0, 0, null);
        g2d.dispose();

        return rotated;
    }

    public Image getCurrentImage() {
        return rotatedImgs[currentImgIndex];
    }

    public void update() {
        x += vx;
        y += vy;

        // 每15帧切换一次图片
        if (random.nextInt(15) == 0) {
            currentImgIndex = (currentImgIndex + 1) % 3;
        }

        // 定期更新旋转角度
        if (random.nextInt(5) == 0) {
            updateRotation();
        }
    }

    public boolean isOutOfBounds(int panelWidth, int panelHeight) {
        return x < -200 || x > panelWidth + 200 ||
                y < -200 || y > panelHeight + 200;
    }
    // 判断鱼是否应该被移除
    public boolean shouldRemove() {
        return isDead;
    }

    // 设置鱼被击中
    public void setDead() {
        isDead = true;
    }
    // 新增获取分数的方法
    public int getScoreValue() {
        return scoreValue;
    }
    // Getters
    public int getX() { return x; }
    public int getY() { return y; }
    public int getWidth() { return originalImgs[0].getWidth(); }
    public int getHeight() { return originalImgs[0].getHeight(); }
}